package com.itheima;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
可变参数：
    数据类型...变量名
    本质：数组
    应用于数据类型相同，数据个数不确定的情况。0~n个
 */
public class Demo_Changeable {
    public static void main(String[] args) {
       /* int a = 10;
        int b = 20;
        int c = 30;
        int result1 = getSum1(a,b);
        int result2 = getSum2(a,b,c);
        int[] arr = {1,2,3,4,5};
        int result3 = getSum3(arr);*/
       //getSum(int...arr)的四种应用：数组，数，null,无参
        /*int[] arr = {1,2,3,4,5};
        int sum = getSum(arr);//数组
        int sum1 = getSum(1,2,3);//数，自动封装数组
        int sum2 = getSum(null);//null
        int sum3 = getSum();//接收0个
        System.out.println(sum+" "+sum1+" "+sum2+" "+sum3);*/
        //List.of...Set.of...Map.of
        /*List<String> list = List.of("a", "b", "c", "d");
        ArrayList<String> arrayList = new ArrayList<>(list);
        list.forEach(s-> System.out.println(s));
        Set<String> set = Set.of("a", "b", "c", "d");
        Map<String, String> map = Map.of("a", "001", "b", "002");
        Map<Object, Object> ofEntries = Map.ofEntries();*/
    }

    //可变参数求和方法getSum
    private static int getSum(int...arr) {
        if(arr==null){
            return 0;
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

   /* private static int getSum3(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    private static int getSum2(int a, int b, int c) {
        return a+b+c;
    }

    private static int getSum1(int a, int b) {
        return a+b;
    }*/
}
